﻿namespace Pub.Class
{
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using System.Runtime.InteropServices;

    public class Database
    {
        protected string connString;
        protected string dbType;
        private DbProviderFactory factory;
        protected string key = "ConnString";
        private readonly object lockHelper = new object();
        private Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
        private IDbProvider provider;
        private long queryCount;

        public Database(string pool)
        {
            this.key = pool;
        }

        private void AssignParameterValues(DbParameter[] commandParameters, DataRow dataRow)
        {
            if (!commandParameters.IsNull() && !dataRow.IsNull())
            {
                int num = 0;
                foreach (DbParameter parameter in commandParameters)
                {
                    if (parameter.ParameterName.IsNull() || (parameter.ParameterName.Length <= 1))
                    {
                        throw new Exception(string.Format("请提供参数{0}一个有效的名称{1}.", num, parameter.ParameterName));
                    }
                    if (dataRow.Table.Columns.IndexOf(parameter.ParameterName.Substring(1)) != -1)
                    {
                        parameter.Value = dataRow[parameter.ParameterName.Substring(1)];
                    }
                    num++;
                }
            }
        }

        private void AssignParameterValues(DbParameter[] commandParameters, object[] parameterValues)
        {
            if (!commandParameters.IsNull() && !parameterValues.IsNull())
            {
                if (commandParameters.Length != parameterValues.Length)
                {
                    throw new ArgumentException("参数值个数与参数不匹配.");
                }
                int index = 0;
                int length = commandParameters.Length;
                while (index < length)
                {
                    if (parameterValues[index] is IDbDataParameter)
                    {
                        IDbDataParameter parameter = (IDbDataParameter) parameterValues[index];
                        if (parameter.Value.IsNull())
                        {
                            commandParameters[index].Value = DBNull.Value;
                        }
                        else
                        {
                            commandParameters[index].Value = parameter.Value;
                        }
                    }
                    else if (parameterValues[index].IsNull())
                    {
                        commandParameters[index].Value = DBNull.Value;
                    }
                    else
                    {
                        commandParameters[index].Value = parameterValues[index];
                    }
                    index++;
                }
            }
        }

        private void AttachParameters(DbCommand command, DbParameter[] commandParameters)
        {
            if (command.IsNull())
            {
                throw new ArgumentNullException("command");
            }
            if (commandParameters.IsNotNull())
            {
                foreach (DbParameter parameter in commandParameters)
                {
                    if (parameter.IsNotNull())
                    {
                        if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && parameter.Value.IsNull())
                        {
                            parameter.Value = DBNull.Value;
                        }
                        command.Parameters.Add(parameter);
                    }
                }
            }
        }

        public void CacheParameterSet(string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            string str = this.ConnString + ":" + commandText;
            this.paramCache[str] = commandParameters;
        }

        private DbParameter[] CloneParameters(DbParameter[] originalParameters)
        {
            DbParameter[] parameterArray = new DbParameter[originalParameters.Length];
            int index = 0;
            int length = originalParameters.Length;
            while (index < length)
            {
                parameterArray[index] = (DbParameter) ((ICloneable) originalParameters[index]).Clone();
                index++;
            }
            return parameterArray;
        }

        public DbCommand CreateCommand(DbConnection connection, string spName, params string[] sourceColumns)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            DbCommand command = this.Factory.CreateCommand();
            command.CommandText = spName;
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            if (sourceColumns.IsNotNull() && (sourceColumns.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                for (int i = 0; i < sourceColumns.Length; i++)
                {
                    spParameterSet[i].SourceColumn = sourceColumns[i];
                }
                this.AttachParameters(command, spParameterSet);
            }
            return command;
        }

        private DbParameter[] DiscoverSpParameterSet(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            DbCommand cmd = connection.CreateCommand();
            cmd.CommandText = spName;
            cmd.CommandType = CommandType.StoredProcedure;
            connection.Open();
            this.Provider.DeriveParameters(cmd);
            connection.Close();
            if (!includeReturnValueParameter)
            {
                cmd.Parameters.RemoveAt(0);
            }
            DbParameter[] array = new DbParameter[cmd.Parameters.Count];
            cmd.Parameters.CopyTo(array, 0);
            foreach (DbParameter parameter in array)
            {
                parameter.Value = DBNull.Value;
            }
            return array;
        }

        public int ExecSql(string commandText)
        {
            return this.ExecSql((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public int ExecSql(string commandText, params DbParameter[] commandParameters)
        {
            return this.ExecSql((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public int ExecSql(out int id, string commandText)
        {
            return this.ExecSql(out id, (commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, (DbParameter[]) null);
        }

        public int ExecSql(CommandType commandType, string commandText)
        {
            return this.ExecSql(commandType, commandText, null);
        }

        public int ExecSql(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.ExecSql(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(CommandType.StoredProcedure, spName);
        }

        public int ExecSql(out int id, CommandType commandType, string commandText)
        {
            return this.ExecSql(out id, commandType, commandText, (DbParameter[]) null);
        }

        public int ExecSql(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                return this.ExecSql(connection, commandType, commandText, commandParameters);
            }
        }

        public int ExecSql(DbConnection connection, CommandType commandType, string commandText)
        {
            return this.ExecSql(connection, commandType, commandText, null);
        }

        public int ExecSql(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.ExecSql(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(connection, CommandType.StoredProcedure, spName);
        }

        public int ExecSql(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.ExecSql(transaction, commandType, commandText, null);
        }

        public int ExecSql(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.ExecSql(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(transaction, CommandType.StoredProcedure, spName);
        }

        public int ExecSql(out int id, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                return this.ExecSql(out id, connection, commandType, commandText, commandParameters);
            }
        }

        public int ExecSql(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return num;
        }

        public int ExecSql(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            return num;
        }

        public int ExecSql(out int id, DbConnection connection, CommandType commandType, string commandText)
        {
            return this.ExecSql(out id, connection, commandType, commandText, null);
        }

        public int ExecSql(out int id, DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.ExecSql(out id, transaction, commandType, commandText, null);
        }

        public int ExecSql(out int id, DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (this.Provider.GetLastIDSQL().Trim() == "")
            {
                throw new ArgumentNullException("GetLastIDSQL is \"\"");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            command.CommandType = CommandType.Text;
            command.CommandText = this.Provider.GetLastIDSQL();
            int.TryParse(command.ExecuteScalar().ToString(), out id);
            this.queryCount += 1L;
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return num;
        }

        public int ExecSql(out int id, DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            command.CommandType = CommandType.Text;
            command.CommandText = this.Provider.GetLastIDSQL();
            int.TryParse(command.ExecuteScalar().ToString(), out id);
            return num;
        }

        public int ExecSqlTypedParams(string spName, DataRow dataRow)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.ExecSql(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(CommandType.StoredProcedure, spName);
        }

        public int ExecSqlTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.ExecSql(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(connection, CommandType.StoredProcedure, spName);
        }

        public int ExecSqlTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.ExecSql(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.ExecSql(transaction, CommandType.StoredProcedure, spName);
        }

        public void ExecuteCommandWithSplitter(string commandText)
        {
            this.ExecuteCommandWithSplitter(commandText, "\r\nGO\r\n");
        }

        public void ExecuteCommandWithSplitter(string commandText, string splitter)
        {
            int num2;
            int startIndex = 0;
        Label_0002:
            num2 = commandText.IndexOf(splitter, startIndex);
            int length = ((num2 > startIndex) ? num2 : commandText.Length) - startIndex;
            string str = commandText.Substring(startIndex, length);
            if (str.Trim().Length > 0)
            {
                try
                {
                    this.ExecSql(CommandType.Text, str);
                }
                catch
                {
                }
            }
            if (num2 != -1)
            {
                startIndex = num2 + splitter.Length;
                if (startIndex < commandText.Length)
                {
                    goto Label_0002;
                }
            }
        }

        public void FillDataSet(CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                this.FillDataSet(connection, commandType, commandText, dataSet, tableNames);
            }
        }

        public void FillDataSet(string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                this.FillDataSet(connection, spName, dataSet, tableNames, parameterValues);
            }
        }

        public void FillDataSet(CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                this.FillDataSet(connection, commandType, commandText, dataSet, tableNames, commandParameters);
            }
        }

        public void FillDataSet(DbConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            this.FillDataSet(connection, commandType, commandText, dataSet, tableNames, null);
        }

        public void FillDataSet(DbConnection connection, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                this.FillDataSet(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, spParameterSet);
            }
            else
            {
                this.FillDataSet(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
            }
        }

        public void FillDataSet(DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            this.FillDataSet(transaction, commandType, commandText, dataSet, tableNames, null);
        }

        public void FillDataSet(DbTransaction transaction, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                this.FillDataSet(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, spParameterSet);
            }
            else
            {
                this.FillDataSet(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
            }
        }

        public void FillDataSet(DbConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            this.FillDataSet(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
        }

        public void FillDataSet(DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            this.FillDataSet(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
        }

        private void FillDataSet(DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                if (tableNames.IsNotNull() && (tableNames.Length > 0))
                {
                    string sourceTable = "Table";
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        if (tableNames[i].IsNull() || (tableNames[i].Length == 0))
                        {
                            throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
                        }
                        adapter.TableMappings.Add(sourceTable, tableNames[i]);
                        sourceTable = sourceTable + ((i + 1)).ToString();
                    }
                }
                adapter.Fill(dataSet);
                command.Parameters.Clear();
            }
            if (mustCloseConnection)
            {
                connection.Close();
            }
        }

        public DbParameter[] GetCachedParameterSet(string commandText)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            string str = this.ConnString + ":" + commandText;
            DbParameter[] parameterArray = this.paramCache[str] as DbParameter[];
            if (parameterArray.IsNull())
            {
                return null;
            }
            return this.CloneParameters(parameterArray);
        }

        public DataSet GetDataSet(string commandText)
        {
            return this.GetDataSet((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public DataSet GetDataSet(CommandType commandType, string commandText)
        {
            return this.GetDataSet(commandType, commandText, null);
        }

        public DataSet GetDataSet(string commandText, params DbParameter[] commandParameters)
        {
            return this.GetDataSet((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public DataSet GetDataSet(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataSet(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(CommandType.StoredProcedure, spName);
        }

        public DataSet GetDataSet(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                return this.GetDataSet(connection, commandType, commandText, commandParameters);
            }
        }

        public DataSet GetDataSet(DbConnection connection, CommandType commandType, string commandText)
        {
            return this.GetDataSet(connection, commandType, commandText, null);
        }

        public DataSet GetDataSet(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataSet(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(connection, CommandType.StoredProcedure, spName);
        }

        public DataSet GetDataSet(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.GetDataSet(transaction, commandType, commandText, null);
        }

        public DataSet GetDataSet(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataSet(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(transaction, CommandType.StoredProcedure, spName);
        }

        public DataSet GetDataSet(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                this.queryCount += 1L;
                command.Parameters.Clear();
                if (mustCloseConnection)
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public DataSet GetDataSet(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                command.Parameters.Clear();
                return dataSet;
            }
        }

        public DataSet GetDataSetTypedParams(string spName, DataRow dataRow)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataSet(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(CommandType.StoredProcedure, spName);
        }

        public DataSet GetDataSetTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataSet(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(connection, CommandType.StoredProcedure, spName);
        }

        public DataSet GetDataSetTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataSet(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataSet(transaction, CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTable(string commandText)
        {
            return this.GetDataTable((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public DataTable GetDataTable(CommandType commandType, string commandText)
        {
            return this.GetDataTable(commandType, commandText, null);
        }

        public DataTable GetDataTable(string commandText, params DbParameter[] commandParameters)
        {
            return this.GetDataTable((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public DataTable GetDataTable(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataTable(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTable(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                return this.GetDataTable(connection, commandType, commandText, commandParameters);
            }
        }

        public DataTable GetDataTable(DbConnection connection, CommandType commandType, string commandText)
        {
            return this.GetDataTable(connection, commandType, commandText, null);
        }

        public DataTable GetDataTable(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataTable(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(connection, CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTable(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.GetDataTable(transaction, commandType, commandText, null);
        }

        public DataTable GetDataTable(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDataTable(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(transaction, CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTable(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataTable dataTable = new DataTable {
                    TableName = "NewTableName"
                };
                adapter.Fill(dataTable);
                this.queryCount += 1L;
                command.Parameters.Clear();
                if (mustCloseConnection)
                {
                    connection.Close();
                }
                return dataTable;
            }
        }

        public DataTable GetDataTable(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataTable dataTable = new DataTable {
                    TableName = "NewTableName"
                };
                adapter.Fill(dataTable);
                command.Parameters.Clear();
                return dataTable;
            }
        }

        public DataTable GetDataTableTypedParams(string spName, DataRow dataRow)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataTable(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTableTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataTable(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(connection, CommandType.StoredProcedure, spName);
        }

        public DataTable GetDataTableTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDataTable(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDataTable(transaction, CommandType.StoredProcedure, spName);
        }

        public DbDataReader GetDbDataReader(string commandText)
        {
            return this.GetDbDataReader((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText);
        }

        public DbDataReader GetDbDataReader(CommandType commandType, string commandText)
        {
            return this.GetDbDataReader(commandType, commandText, null);
        }

        public DbDataReader GetDbDataReader(string commandText, params DbParameter[] commandParameters)
        {
            return this.GetDbDataReader((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public DbDataReader GetDbDataReader(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDbDataReader(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDbDataReader(CommandType.StoredProcedure, spName);
        }

        public DbDataReader GetDbDataReader(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            DbDataReader reader;
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            DbConnection connection = null;
            try
            {
                connection = this.Factory.CreateConnection();
                connection.ConnectionString = this.ConnString;
                connection.Open();
                reader = this.GetDbDataReader(connection, null, commandType, commandText, commandParameters, DbConnectionOwnership.Internal);
            }
            catch
            {
                if (connection.IsNotNull())
                {
                    connection.Close();
                }
                throw;
            }
            return reader;
        }

        public DbDataReader GetDbDataReader(DbConnection connection, CommandType commandType, string commandText)
        {
            return this.GetDbDataReader(connection, commandType, commandText, null);
        }

        public DbDataReader GetDbDataReader(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDbDataReader(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDbDataReader(connection, CommandType.StoredProcedure, spName);
        }

        public DbDataReader GetDbDataReader(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.GetDbDataReader(transaction, commandType, commandText, null);
        }

        public DbDataReader GetDbDataReader(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetDbDataReader(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDbDataReader(transaction, CommandType.StoredProcedure, spName);
        }

        public DbDataReader GetDbDataReader(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            return this.GetDbDataReader(connection, null, commandType, commandText, commandParameters, DbConnectionOwnership.External);
        }

        public DbDataReader GetDbDataReader(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            return this.GetDbDataReader(transaction.Connection, transaction, commandType, commandText, commandParameters, DbConnectionOwnership.External);
        }

        private DbDataReader GetDbDataReader(DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters, DbConnectionOwnership connectionOwnership)
        {
            DbDataReader reader2;
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            bool mustCloseConnection = false;
            DbCommand command = this.Factory.CreateCommand();
            try
            {
                DbDataReader reader;
                this.PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
                if (connectionOwnership == DbConnectionOwnership.External)
                {
                    reader = command.ExecuteReader();
                }
                else
                {
                    reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                }
                this.queryCount += 1L;
                bool flag2 = true;
                foreach (DbParameter parameter in command.Parameters)
                {
                    if (parameter.Direction != ParameterDirection.Input)
                    {
                        flag2 = false;
                    }
                }
                if (flag2)
                {
                    command.Parameters.Clear();
                }
                reader2 = reader;
            }
            catch
            {
                if (mustCloseConnection)
                {
                    connection.Close();
                }
                throw;
            }
            return reader2;
        }

        public DbDataReader GetDbDataReaderTypedParams(string spName, DataRow dataRow)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDbDataReader(this.ConnString, new object[] { CommandType.StoredProcedure, spName, spParameterSet });
            }
            return this.GetDbDataReader(this.ConnString, new object[] { CommandType.StoredProcedure, spName });
        }

        public DbDataReader GetDbDataReaderTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDbDataReader(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDbDataReader(connection, CommandType.StoredProcedure, spName);
        }

        public DbDataReader GetDbDataReaderTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetDbDataReader(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetDbDataReader(transaction, CommandType.StoredProcedure, spName);
        }

        public string GetIdentifierEnd()
        {
            return this.Provider.GetIdentifierEnd();
        }

        public string GetIdentifierStart()
        {
            return this.Provider.GetIdentifierStart();
        }

        public string GetParamIdentifier()
        {
            return this.Provider.GetParamIdentifier();
        }

        public object GetScalar(string commandText)
        {
            return this.GetScalar((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText);
        }

        public object GetScalar(CommandType commandType, string commandText)
        {
            return this.GetScalar(commandType, commandText, null);
        }

        public object GetScalar(string commandText, params DbParameter[] commandParameters)
        {
            return this.GetScalar((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public object GetScalar(string spName, params object[] parameterValues)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                if (connection.IsNull())
                {
                    throw new ArgumentNullException("connection");
                }
                if (spName.IsNull() || (spName.Length == 0))
                {
                    throw new ArgumentNullException("spName");
                }
                if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
                {
                    DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                    this.AssignParameterValues(spParameterSet, parameterValues);
                    return this.GetScalar(connection, spName, (object[]) spParameterSet);
                }
                return this.GetScalar(CommandType.StoredProcedure, spName);
            }
        }

        public object GetScalar(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                connection.Open();
                return this.GetScalar(connection, commandType, commandText, commandParameters);
            }
        }

        public object GetScalar(DbConnection connection, CommandType commandType, string commandText)
        {
            return this.GetScalar(connection, commandType, commandText, null);
        }

        public object GetScalar(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetScalar(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetScalar(connection, CommandType.StoredProcedure, spName);
        }

        public object GetScalar(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return this.GetScalar(transaction, commandType, commandText, null);
        }

        public object GetScalar(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, parameterValues);
                return this.GetScalar(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetScalar(transaction, CommandType.StoredProcedure, spName);
        }

        public object GetScalar(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            object obj2 = command.ExecuteScalar();
            command.Parameters.Clear();
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return obj2;
        }

        public object GetScalar(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = this.Factory.CreateCommand();
            bool mustCloseConnection = false;
            this.PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            object obj2 = command.ExecuteScalar();
            this.queryCount += 1L;
            command.Parameters.Clear();
            return obj2;
        }

        public string GetScalarToStr(CommandType commandType, string commandText)
        {
            object scalar = this.GetScalar(commandType, commandText);
            if (scalar.IsNull())
            {
                return "";
            }
            return scalar.ToString();
        }

        public string GetScalarToStr(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            object obj2 = this.GetScalar(commandType, commandText, commandParameters);
            if (obj2.IsNull())
            {
                return "";
            }
            return obj2.ToString();
        }

        public object GetScalarTypedParams(string spName, DataRow dataRow)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetScalar(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetScalar(CommandType.StoredProcedure, spName);
        }

        public object GetScalarTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetScalar(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetScalar(connection, CommandType.StoredProcedure, spName);
        }

        public object GetScalarTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = this.GetSpParameterSet(transaction.Connection, spName);
                this.AssignParameterValues(spParameterSet, dataRow);
                return this.GetScalar(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return this.GetScalar(transaction, CommandType.StoredProcedure, spName);
        }

        public DbParameter[] GetSpParameterSet(string spName)
        {
            return this.GetSpParameterSet(spName, false);
        }

        internal DbParameter[] GetSpParameterSet(DbConnection connection, string spName)
        {
            return this.GetSpParameterSet(connection, spName, false);
        }

        public DbParameter[] GetSpParameterSet(string spName, bool includeReturnValueParameter)
        {
            if (this.ConnString.IsNull() || (this.ConnString.Length == 0))
            {
                throw new ArgumentNullException(this.key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            using (DbConnection connection = this.Factory.CreateConnection())
            {
                connection.ConnectionString = this.ConnString;
                return this.GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
            }
        }

        internal DbParameter[] GetSpParameterSet(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            using (DbConnection connection2 = (DbConnection) ((ICloneable) connection).Clone())
            {
                return this.GetSpParameterSetInternal(connection2, spName, includeReturnValueParameter);
            }
        }

        private DbParameter[] GetSpParameterSetInternal(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            string str = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
            DbParameter[] parameterArray = this.paramCache[str] as DbParameter[];
            if (parameterArray.IsNull())
            {
                DbParameter[] parameterArray2 = this.DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
                this.paramCache[str] = parameterArray2;
                parameterArray = parameterArray2;
            }
            return this.CloneParameters(parameterArray);
        }

        public DbParameter MakeInParam(string ParamName, DbType DbType, int Size, object Value)
        {
            return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
        }

        public DbParameter MakeOutParam(string ParamName, DbType DbType, int Size)
        {
            return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
        }

        public DbParameter MakeParam(string ParamName, DbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            DbParameter parameter = this.Provider.MakeParam(ParamName, DbType, Size);
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || !Value.IsNull())
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        private void PrepareCommand(DbCommand command, DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters, out bool mustCloseConnection)
        {
            if (command.IsNull())
            {
                throw new ArgumentNullException("command");
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            if (connection.State != ConnectionState.Open)
            {
                mustCloseConnection = true;
                connection.Open();
            }
            else
            {
                mustCloseConnection = false;
            }
            command.Connection = connection;
            command.CommandText = commandText;
            if (transaction.IsNotNull())
            {
                if (transaction.Connection.IsNull())
                {
                    throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
                }
                command.Transaction = transaction;
            }
            command.CommandType = commandType;
            if (commandParameters.IsNotNull())
            {
                this.AttachParameters(command, commandParameters);
            }
        }

        public void ResetDbProvider()
        {
            this.connString = null;
            this.factory = null;
            this.provider = null;
            this.dbType = null;
            this.key = null;
        }

        public void UpdateDataSet(DbCommand insertCommand, DbCommand deleteCommand, DbCommand updateCommand, DataSet dataSet, string tableName)
        {
            if (insertCommand.IsNull())
            {
                throw new ArgumentNullException("insertCommand");
            }
            if (deleteCommand.IsNull())
            {
                throw new ArgumentNullException("deleteCommand");
            }
            if (updateCommand.IsNull())
            {
                throw new ArgumentNullException("updateCommand");
            }
            if (tableName.IsNull() || (tableName.Length == 0))
            {
                throw new ArgumentNullException("tableName");
            }
            using (DbDataAdapter adapter = this.Factory.CreateDataAdapter())
            {
                adapter.UpdateCommand = updateCommand;
                adapter.InsertCommand = insertCommand;
                adapter.DeleteCommand = deleteCommand;
                adapter.Update(dataSet, tableName);
                dataSet.AcceptChanges();
            }
        }

        public string ConnString
        {
            get
            {
                if (string.IsNullOrEmpty(this.connString) && ConfigurationManager.ConnectionStrings[this.key].IsNotNull())
                {
                    this.connString = ConfigurationManager.ConnectionStrings[this.key].ToString();
                    this.dbType = ConfigurationManager.ConnectionStrings[this.key].ProviderName;
                }
                return this.connString;
            }
            set
            {
                this.connString = value;
            }
        }

        public string DBType
        {
            get
            {
                if (string.IsNullOrEmpty(this.dbType))
                {
                    if (ConfigurationManager.ConnectionStrings[this.key].IsNotNull())
                    {
                        this.dbType = ConfigurationManager.ConnectionStrings[this.key].ProviderName;
                    }
                    else
                    {
                        this.dbType = "SqlServer";
                    }
                }
                return this.dbType;
            }
            set
            {
                this.dbType = value;
            }
        }

        public DbProviderFactory Factory
        {
            get
            {
                if (this.factory.IsNull())
                {
                    this.factory = this.Provider.Instance();
                }
                return this.factory;
            }
        }

        public string Pool
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }

        public IDbProvider Provider
        {
            get
            {
                if (this.provider.IsNull())
                {
                    lock (this.lockHelper)
                    {
                        if (this.provider.IsNull())
                        {
                            this.dbType = this.DBType;
                            try
                            {
                                this.provider = (IDbProvider) "Pub.Class.{0},Pub.Class.{0}".FormatWith(this.dbType).LoadClass();
                            }
                            catch
                            {
                                throw new Exception(this.dbType + " - 请检查web.config中DbType节点数据库类型是否正确，例如：SqlServer、Access、MySql、OleDb、Odbc");
                            }
                        }
                    }
                }
                return this.provider;
            }
        }

        public long QueryCount
        {
            get
            {
                return this.queryCount;
            }
            set
            {
                this.queryCount = value;
            }
        }

        private enum DbConnectionOwnership
        {
            Internal,
            External
        }
    }
}

